package com.bhs.sansong.ui.orderDispose;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.ArrayMap;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.bhs.sansong.R;
import com.bhs.sansong.adapter.PictureSelectAdapter;
import com.bhs.sansong.base.BaseActivity;
import com.bhs.sansong.base.BaseBean;
import com.bhs.sansong.bean.AdvancePayment;
import com.bhs.sansong.bean.TabBean;
import com.bhs.sansong.net.NetCallBack;
import com.bhs.sansong.utils.ToastUtil;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import okhttp3.ResponseBody;
import retrofit2.Response;

/**
 * 投诉维权
 */
public class ComplaintActivity extends BaseActivity implements NetCallBack, View.OnClickListener {


    private String deliver_sn;
    private TextView mTvCancel;
    private RecyclerView mRvType;
    private List<TabBean> typeList = new ArrayList<>();
    private BaseQuickAdapter<TabBean, BaseViewHolder> mAdapterFeedback;
    private RecyclerView mRvPicture;
    private EditText mEtContent;
    private Button mBtnSubmit;
    private PictureSelectAdapter selectAdapter;
    private String type;
    private TextView mTvRecord;

    @Override
    public int initLayout() {
        return R.layout.activity_complaint;
    }

    @Override
    public void initView() {
        deliver_sn = getIntent().getStringExtra("deliver_sn");
        mTvCancel = findViewById(R.id.tv_cancel);
        mRvType = findViewById(R.id.rv_type);
        mRvPicture = findViewById(R.id.rv_picture);
        mEtContent = findViewById(R.id.et_content);
        mBtnSubmit = findViewById(R.id.btn_submit);
        mTvCancel.setOnClickListener(this);
        mBtnSubmit.setOnClickListener(this);
        networkRequest(netApi.get_abnormal_num(deliver_sn), this, 555);
        mTvRecord = findViewById(R.id.tv_record);
        mTvRecord.setOnClickListener(this);
    }

    @Override
    public void initData() {
        selectAdapter = new PictureSelectAdapter(3, mActivity);
        mRvPicture.setAdapter(selectAdapter);
        mRvPicture.setLayoutManager(new GridLayoutManager(mActivity, 3));
        typeList.add(new TabBean("服务态度"));
        typeList.add(new TabBean("运输超时"));
        typeList.add(new TabBean("回单超时"));
        typeList.add(new TabBean("货物破损"));
        typeList.add(new TabBean("货物丢失"));
        typeList.add(new TabBean("其他"));
        mAdapterFeedback = new BaseQuickAdapter<TabBean, BaseViewHolder>(R.layout.item_line, typeList) {
            @Override
            protected void convert(@NonNull BaseViewHolder helper, TabBean item) {
                TextView tvName = helper.getView(R.id.tv_name);
                tvName.setText(item.getTypeName());
                tvName.setTextColor(Color.parseColor(item.isChecked() ? "#485EF4" : "#000000"));
                tvName.setBackgroundResource(item.isChecked() ? R.drawable.bg_pale_blue : R.drawable.bg_grey_4);
            }
        };
        mRvType.setAdapter(mAdapterFeedback);
        mRvType.setLayoutManager(new GridLayoutManager(mActivity, 3));
        mAdapterFeedback.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @SuppressLint("NotifyDataSetChanged")
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                type = mAdapterFeedback.getData().get(position).getTypeName();
                for (TabBean datum : mAdapterFeedback.getData()) {
                    datum.setChecked(false);
                }
                mAdapterFeedback.getData().get(position).setChecked(true);
                mAdapterFeedback.notifyDataSetChanged();
                verify();
            }
        });
        mEtContent.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                verify();
            }
        });
    }

    private void verify() {
        boolean verify = true;
        if (TextUtils.isEmpty(mEtContent.getText())) {
            verify = false;
        }
        if (TextUtils.isEmpty(type)) {
            verify = false;
        }

        mBtnSubmit.setEnabled(verify);
        mBtnSubmit.setBackgroundColor(verify ? Color.parseColor("#485EF4") : Color.parseColor("#7F485EF4"));
    }

    @Override
    public void onResponse(int requestCode, String json, Response<ResponseBody> response) throws Exception {
        AdvancePayment bean = new Gson().fromJson(json, AdvancePayment.class);
        if (requestCode == 0) {
            if (bean.isStatus()) {
                finish();
                ToastUtil.show("投诉成功");
            } else {
                ToastUtil.show(bean.getMsg());
            }
        } else {
            if (bean.isStatus()) {
                mTvRecord.setText(String.format("维权记录(%s)", bean.getData().getNum()));
            }
        }
    }

    @Override
    public void onFailure(int requestCode) {

    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.btn_submit) {
            Map<String, String> map = new ArrayMap<>();
            map.put("deliver_sn", deliver_sn);
            map.put("type", type);
            map.put("content", mEtContent.getText().toString());
            map.put("imgs", selectAdapter.getImages());
            networkRequest(netApi.apply_abnormal(map), this);
        } else {
            finish();
        }
    }
}